package com.huaboot.system.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huaboot.commont.page.PageResult;
import com.huaboot.system.convert.SysObjectStoreConvert;
import com.huaboot.system.entity.SysObjectStoreEntity;
import com.huaboot.system.query.SysObjectStoreQuery;
import com.huaboot.system.vo.SysObjectStoreVO;
import com.huaboot.system.mapper.SysObjectStoreMapper;
import com.huaboot.system.service.SysObjectStoreService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import cn.hutool.core.util.ObjectUtil;




/**
 * 对象存储表
 *
 * @author ff xxx
 * @since 1.0.0 2023-09-28
 */
@Service
@AllArgsConstructor
public class SysObjectStoreServiceImpl extends ServiceImpl<SysObjectStoreMapper, SysObjectStoreEntity> implements SysObjectStoreService {

    @Override
    public PageResult<SysObjectStoreVO> page(SysObjectStoreQuery query) {
        PageHelper.startPage(query.currentPage, query.size);
        List<SysObjectStoreEntity> SysObjectStoreEntityList = list(getWrapper(query));
        PageInfo<SysObjectStoreEntity> page = new PageInfo<>(SysObjectStoreEntityList);
        return new PageResult<>(SysObjectStoreConvert.INSTANCE.convertList(page.getList()), page.getTotal());
    }

    private LambdaQueryWrapper<SysObjectStoreEntity> getWrapper(SysObjectStoreQuery query){
        LambdaQueryWrapper<SysObjectStoreEntity> wrapper = Wrappers.lambdaQuery();
        return wrapper;
    }

    @Override
    public void save(SysObjectStoreVO vo) {
        SysObjectStoreEntity entity = SysObjectStoreConvert.INSTANCE.convert(vo);
         save(entity);
    }

    @Override
    public void update(SysObjectStoreVO vo) {
        SysObjectStoreEntity entity = SysObjectStoreConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public SysObjectStoreEntity getObjectStoreByLabel(String objectStoreLabel) {
        return baseMapper.selectOne(new LambdaQueryWrapper<SysObjectStoreEntity>()
                .eq(SysObjectStoreEntity::getObjectStoreLabel, objectStoreLabel));
    }

    @Override
    @Cacheable(cacheNames = "STRATEGY-NAME")
    public String getCurrentStrategyName() {
        LambdaQueryWrapper<SysObjectStoreEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysObjectStoreEntity::getEnableState,1);
        SysObjectStoreEntity sysObjectStoreEntity = baseMapper.selectOne(wrapper);
        return sysObjectStoreEntity.getObjectStoreLabel();
    }

}
